<template>
  <a-form @submit="handleSubmit" :form="form" class="form">
    <a-row class="form-row" :gutter="16">
      <a-col :lg="6" :md="12" :sm="24">
        <a-form-item label="行业门类">
          <a-select :disabled="disabled" @change="changeCategory" placeholder="请选择行业门类" v-decorator="[ 'category', {rules: [{ required: false, message: '请选择行业门类'}]} ]">
            <a-select-option v-for="item in CategoryData" :key="item.id.toString()" :value="item.id.toString()">{{item.label}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      <a-col :lg="6" :md="12" :sm="24">
        <a-form-item label="行业大类">
          <a-select :disabled="disabled" @change="changeSpecific"  placeholder="请选择行业大类" v-decorator="[ 'specific', {rules: [{ required: false, message: '请选择行业大类'}]} ]">
            <a-select-option v-for="item in specificData" :key="item.id.toString()" :value="item.id.toString()">{{item.label}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      <a-col :lg="6" :md="12" :sm="24">
        <a-form-item label="行业中类">
          <a-select :disabled="disabled" @change="changeTrade"  placeholder="请选择行业中类" v-decorator="[ 'trade', {rules: [{ required: false, message: '请选择行业中类'}]} ]">
            <a-select-option v-for="item in TradeData" :key="item.id.toString()" :value="item.id.toString()">{{item.label}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      <a-col :lg="6" :md="12" :sm="24">
        <a-form-item label="行业小类">
          <a-select :disabled="disabled" placeholder="请选择行业小类" v-decorator="[ 'subclass', {rules: [{ required: false, message: '请选择行业小类'}]} ]">
            <a-select-option v-for="item in subclassData" :key="item.id.toString()" :value="item.id.toString()">{{item.label}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
    </a-row>
    <a-form-item v-if="showSubmit">
      <a-button htmlType="submit" >Submit</a-button>
    </a-form-item>
  </a-form>
</template>

<script>
  import {listIndustryClassification} from '@/api/classification';
  import {getServindustryInfo} from '@/api/organization';
export default {
  name: 'ServindustryForm',
  props: {
    showSubmit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: this.$form.createForm(this),
      disabled: false,
      CategoryData:[],
      specificData:[],
      TradeData:[],
      subclassData:[],
      id: '',
    }
  },
  mounted() {
    this.listIndustryClassification(0);
  },
  methods: {
    getDetail(obj) {
      const {type, value} = obj
      if (type === 'view') {
        this.disabled = true
        this.getInfo(JSON.parse(value).gnrlid)
      } else if (type === 'edit') {
        this.disabled = false;
        this.getInfo(JSON.parse(value).gnrlid)
      } else if (type === 'add') {
        this.disabled = false;
        this.form.resetFields()
        this.id = ''
      }
    },
    getInfo(gnrlid){
      let that = this
      getServindustryInfo(gnrlid).then(res => {
        const data = res.data
        const {category, specific, trade} = data
        if (category) {
          listIndustryClassification(category).then(res => {
            this.specificData = res.data
          })
        }
        if (specific) {
          listIndustryClassification(specific).then(res => {
            this.TradeData = res.data
          })
        }
        if (trade) {
          listIndustryClassification(trade).then(res => {
            this.subclassData = res.data
          })
        }
        that.$nextTick(()=>{
          setTimeout(() =>{
            that.id = data.id
            that.form.setFieldsValue(data)
          },0)
        });
      });
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.$notification['error']({
            message: 'Received values of form:',
            description: values
          })
        }
      })
    },
    listIndustryClassification(id){
      listIndustryClassification(id).then(res => {
        this.CategoryData = res.data
        this.specificData = []
        this.TradeData = []
        this.subclassData = []
      })
    },
    changeCategory(id){
      listIndustryClassification(id).then(res => {
        this.specificData = res.data
        this.TradeData = []
        this.subclassData = []
        this.form.setFieldsValue({specific: '',Trade: '',subclass: ''})
      })
    },
    changeSpecific(id){
      listIndustryClassification(id).then(res => {
        this.TradeData = res.data
        this.subclassData = []
        this.form.setFieldsValue({Trade: '',subclass: ''})
      })
    },
    changeTrade(id){
      listIndustryClassification(id).then(res => {
        this.form.setFieldsValue({subclass: ''})
        this.subclassData = res.data
      })
    }
  }
}
</script>

<style scoped>

</style>
